package com.net.shop.car.manager.ui.area;

import java.util.List;

import android.animation.LayoutTransition;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.net.shop.car.manager.R;
import com.net.shop.car.manager.api.model.Area;
import com.net.shop.car.manager.api.volley.VolleyCenter;
import com.net.shop.car.manager.api.volley.VolleyListenerImpl;
import com.net.shop.car.manager.api.volley.request.City;
import com.net.shop.car.manager.api.volley.request.Country;
import com.net.shop.car.manager.api.volley.request.Province;
import com.net.shop.car.manager.api.volley.response.CityResponse;
import com.net.shop.car.manager.api.volley.response.CountryResponse;
import com.net.shop.car.manager.api.volley.response.ProvinceResponse;
import com.net.shop.car.manager.ui.FullScreenDialog;

/**
 * 选中您的地区，省，市，县
 * 
 * @author Deve
 * 
 */
public class AreaChoose extends FullScreenDialog {
	/**
	 * 
	 */
	private TextView titleTextView;
	/**
	 * 
	 */
	private FrameLayout frameLayout;

	private int title;

	public static final int TITLE_PROVINCE = 1;

	public static final int TITLE_CITY = 2;

	public static final int TITLE_COUNTRY = 3;
	/**
	 * 
	 */
	private Area selectedProvince;

	private Area selectedCity;

	private Area selectedCountry;

	public AreaChoose(Context context) {
		super(context, R.style.Dialog_Fullscreen);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.dialog_area_choose);

		title = TITLE_PROVINCE;

		initViews();

	}

	private void initViews() {
		frameLayout = (FrameLayout) findViewById(R.id.area_choose_container);

		titleTextView = (TextView) findViewById(R.id.title_txt);

		findViewById(R.id.title_back_btn).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						onBackPressed();
					}
				});

		LayoutTransition mTransition = new LayoutTransition();
		
		mTransition.setAnimator(LayoutTransition.APPEARING,
				(mTransition.getAnimator(LayoutTransition.APPEARING)));
		mTransition.setAnimator(LayoutTransition.CHANGE_APPEARING,
				(mTransition.getAnimator(LayoutTransition.CHANGE_APPEARING)));
		mTransition.setAnimator(LayoutTransition.DISAPPEARING,
				(mTransition.getAnimator(LayoutTransition.DISAPPEARING)));
		
		mTransition
				.setAnimator(LayoutTransition.CHANGE_DISAPPEARING, (mTransition
						.getAnimator(LayoutTransition.CHANGE_DISAPPEARING)));
		frameLayout.setLayoutTransition(mTransition);
		
		freshCurrentList();
	}

	private void freshCurrentList() {
		final AreaList areaList = new AreaList(activity.inflater, activity,
				frameLayout);
		freshTitle();
		switch (title) {
		case TITLE_PROVINCE:
			VolleyCenter.getVolley().addPostRequest(
					new Province(),
					new VolleyListenerImpl<ProvinceResponse>(
							new ProvinceResponse()) {

						@Override
						public void deleveryResponse(ProvinceResponse response) {
							if (response.isSuccess()) {
								final List<Area> list = response
										.getProvince();
								areaList.setData(list);
								areaList.setOnItemClickListener(new OnItemClickListener() {

									@Override
									public void onItemClick(
											AdapterView<?> parent, View view,
											int position, long id) {
										selectedProvince = list.get(position);
										title = TITLE_CITY;
										freshCurrentList();
									}
								});
							} else {
								// showToast(response.getErrorMsg());
							}
						}
					});
			break;
		case TITLE_CITY:
			City city = new City(selectedProvince.getId());
			VolleyCenter.getVolley().addPostRequest(city,
					new VolleyListenerImpl<CityResponse>(new CityResponse()) {

						@Override
						public void deleveryResponse(CityResponse response) {
							if (response.isSuccess()) {
								final List<Area> list = response
										.getProvince();
								areaList.setData(list);
								areaList.setOnItemClickListener(new OnItemClickListener() {

									@Override
									public void onItemClick(
											AdapterView<?> parent, View view,
											int position, long id) {
										selectedCity = list.get(position);
										title = TITLE_COUNTRY;
										freshCurrentList();
									}
								});
							} else {
								// showToast(response.getErrorMsg());
							}
						}
					});
			break;
		case TITLE_COUNTRY:
			Country country = new Country(selectedCity.getId());
			VolleyCenter.getVolley().addPostRequest(
					country,
					new VolleyListenerImpl<CountryResponse>(
							new CountryResponse()) {

						@Override
						public void deleveryResponse(CountryResponse response) {
							if (response.isSuccess()) {
								final List<Area> list = response
										.getProvince();
								areaList.setData(list);
								areaList.setOnItemClickListener(new OnItemClickListener() {

									@Override
									public void onItemClick(
											AdapterView<?> parent, View view,
											int position, long id) {
										selectedCountry = list.get(position);
										dismiss();
									}
								});
							} else {
								// showToast(response.getErrorMsg());
							}
						}
					});
			break;
		default:
			break;
		}
	}

	private void freshTitle() {
		String titleStr = "";
		switch (title) {
		case TITLE_PROVINCE:
			titleStr = activity.getString(R.string.povincechoose_title);
			break;
		case TITLE_CITY:
			titleStr = activity.getString(R.string.citycheck_title);
			break;
		case TITLE_COUNTRY:
			titleStr = activity.getString(R.string.countrychoose_title);
			break;
		}
		titleTextView.setText(titleStr);
	}
	
	@Override
	public void onBackPressed() {
		int size = frameLayout.getChildCount();
		if (size > 1) {
			frameLayout.removeViewAt(size - 1);
			title --;
			freshTitle();
		} else {
			super.onBackPressed();
		}
	}

}
